﻿#ifndef __RC522_H__
#define __RC522_H__

#include "gd32e23x.h"
#include "uPin.h"

// #define RC522_RESET_GPIO_CLK    RCC_APB2Periph_GPIOB
#define RC522_RESET_SET     gpio_bit_set(RC522_RESET_IO, RC522_RESET_PIN)      //1
#define RC522_RESET_CLR     gpio_bit_reset(RC522_RESET_IO, RC522_RESET_PIN)    //0

#define RC522_ENABLE 		gpio_bit_reset(RC522_CS_IO,GPIO_PIN_12)
#define RC522_DISABLE 	    gpio_bit_set(RC522_CS_IO,GPIO_PIN_12)


/////////////////////////////////////////////////////////////////////
// MF522命令字
/////////////////////////////////////////////////////////////////////
#define PCD_IDLE 0x00       // 取消当前命令
#define PCD_AUTHENT 0x0E    // 验证密钥
#define PCD_RECEIVE 0x08    // 接受数据
#define PCD_TRANSMIT 0x04   // 发送数据
#define PCD_TRANSCEIVE 0x0C // 发送并接收数据
#define PCD_RESETPHASE 0x0F // 复位
#define PCD_CALCCRC 0x03    // CRC计算

/////////////////////////////////////////////////////////////////////
// Mifare_One 卡片命令字
/////////////////////////////////////////////////////////////////////
#define PICC_REQIDL 0x26    // 寻天线区内未进入休眠状态
#define PICC_REQALL 0x52    // 寻天线区内全部卡
#define PICC_ANTICOLL1 0x93 // 防冲撞
#define PICC_ANTICOLL2 0x95 // 防冲撞
#define PICC_AUTHENT1A 0x60 // 验证A密钥
#define PICC_AUTHENT1B 0x61 // 验证B密钥
#define PICC_READ 0x30      // 读块
#define PICC_WRITE 0xA0     // 写块
// #define PICC_DECREMENT     0xC0               // 扣款
// #define PICC_INCREMENT     0xC1               // 充值
#define PICC_RESTORE 0xC2  // 调块数据到缓冲区
#define PICC_TRANSFER 0xB0 // 保存缓冲区中数据
#define PICC_HALT 0x50     // 休眠

/////////////////////////////////////////////////////////////////////
// MF522 FIFO?长度定义
/////////////////////////////////////////////////////////////////////
#define DEF_FIFO_LENGTH 64 // FIFO size=64byte

/////////////////////////////////////////////////////////////////////
// MF522寄存器定义
/////////////////////////////////////////////////////////////////////

// PAGE 0 命令和状态
#define RFU00 0x00         // 保留
#define CommandReg 0x01    // 启动和停止命令的执行
#define ComIEnReg 0x02     // 中断请求传递的使能和禁能控制位
#define DivlEnReg 0x03     // 中断请求传递的使能和禁能控制位
#define ComIrqReg 0x04     // 包含中断请求标志
#define DivIrqReg 0x05     // 包含中断请求标志
#define ErrorReg 0x06      // 错误标志，指示执行的上个命令的错误状态
#define Status1Reg 0x07    // 包含通信的状态标志
#define Status2Reg 0x08    // 包含接收器和发送器的状态标志
#define FIFODataReg 0x09   // 64字节FIFO缓存区的输入和输出
#define FIFOLevelReg 0x0A  // 指示FIFO中的存储的字节数
#define WaterLevelReg 0x0B // 定义FIFO下溢和上溢报警的FIFO深度
#define ControlReg 0x0C    // 不同的控制寄存器
#define BitFramingReg 0x0D // 面向位的帧的调节
#define CollReg 0x0E       // RF接口上检测到的第一个位冲突的位的位置
#define RFU0F 0x0F         // 保留

// PAGE 1 命令和状态
#define RFU10 0x10          // ???
#define ModeReg 0x11        // ???????????????????
#define TxModeReg 0x12      // ????????????????????
#define RxModeReg 0x13      // ????????????????????
#define TxControlReg 0x14   // ??????????????TX1??X2?????????
#define TxAutoReg 0x15      // ???????????????
#define TxSelReg 0x16       // ?????????????????
#define RxSelReg 0x17       // ???????????????
#define RxThresholdReg 0x18 // ??????????????
#define DemodReg 0x19       // ????????????
#define RFU1A 0x1A          // ???
#define RFU1B 0x1B          // ???
#define MifareReg 0x1C      // ???ISO14443/MIFARE?????106Kbit/s ?????
#define RFU1D 0x1D          // ???
#define RFU1E 0x1E          // ???
#define SerialSpeedReg 0x1F // ???????ART????????

// PAGE 2 CFG
#define RFU20 0x20             // ???
#define CRCResultRegM 0x21     // ???CRC????????SB??SB??
#define CRCResultRegL 0x22     //
#define RFU23 0x23             // ???
#define ModWidthReg 0x24       // ???ModWidth?????
#define RFU25 0x25             // ???
#define RFCfgReg 0x26          // ???????????
#define GsNReg 0x27            // ??????????????TX1??X2????????
#define CWGsCfgReg 0x28        // ??????????????TX1??X2????????
#define ModGsCfgReg 0x29       // ??????????????TX1??X2????????
#define TModeReg 0x2A          // ???????????????
#define TPrescalerReg 0x2B     // ???????????????
#define TReloadRegH 0x2C       // ???16???????????????
#define TReloadRegL 0x2D       // ???16???????????????
#define TCounterValueRegH 0x2E // ???16??????????????
#define TCounterValueRegL 0x2F // ???16??????????????

// PAGE 3 ???
#define RFU30 0x30           // ???
#define TestSel1Reg 0x31     // ??????????????
#define TestSel2Reg 0x32     // ????????????????RBS???
#define TestPinEnReg 0x33    // ????????????????????????????????
#define TestPinValueReg 0x34 // ???D1-D7???I/O????????
#define TestBusReg 0x35      // ?????????????????
#define AutoTestReg 0x36     // ???????????
#define VersionReg 0x37      // ??????
#define AnalogTestReg 0x38   // ??????AUX1??UX2
#define TestDAC1Reg 0x39     // ???TestDAC1???????
#define TestDAC2Reg 0x3A     // ???TestDAC2???????
#define TestADCReg 0x3B      // ???ADC I?? ??????????
#define RFU3C 0x3C           // ???
#define RFU3D 0x3D           // ???
#define RFU3E 0x3E           // ???
#define RFU3F 0x3F           // ???

/////////////////////////////////////////////////////////////////////
//和F522通讯时返回的错误代码
/////////////////////////////////////////////////////////////////////
#define MI_OK 0
#define MI_NOTAGERR 1 //(-1)
#define MI_ERR 2      //(-2)

#define MAXRLEN 64

void InitRC522(void);
extern unsigned char Pcb; // CPU??APDU????????

char PcdRequest(unsigned char req_code, unsigned char *pTagType);
char PcdAnticoll(unsigned char *pSnr);
char PcdSelect(unsigned char *pSnr, unsigned char *sak);
char PcdAuthState(unsigned char auth_mode, unsigned char addr, unsigned char *pKey, unsigned char *pSnr);
char PcdRead(unsigned char addr, unsigned char *pData);
char PcdWrite(unsigned char addr, unsigned char *pData);
char PcdValue(unsigned char dd_mode, unsigned char addr, unsigned char *pValue);
char PcdBakValue(unsigned char sourceaddr, unsigned char goaladdr);
char PcdHalt(void);
void CalulateCRC(unsigned char *pIndata, unsigned char len, unsigned char *pOutData);
char PcdReset(void);
unsigned char ReadRawRC(unsigned char Address);
void WriteRawRC(unsigned char Address, unsigned char value);
void SetBitMask(unsigned char reg, unsigned char mask);
void ClearBitMask(unsigned char reg, unsigned char mask);
char PcdComMF522(unsigned char Command,
                 unsigned char *pInData,
                 unsigned char InLenByte,
                 unsigned char *pOutData,
                 unsigned int *pOutLenBit);
void PcdAntennaOn(void);
void PcdAntennaOff(void);
void PcdAntReset(void);
void RC522_Config(unsigned char Card_Type);

void RC522_SPI_Check(void);
char RC522_Maintask_Check(void);
void DoRc522CheckTask(void);
// CPU?????
unsigned short CardReset(unsigned char *Data_Out, unsigned char *Len);
char PcdRats(unsigned char *DataOut, unsigned char *Len);
unsigned char Pcd_Cmd(unsigned char *pDataIn, unsigned char In_Len, unsigned char *pDataOut, unsigned char *Out_Len);
void PcdSwitchPCB(void);

#endif
